﻿/*
 *
 * Checking.cs
 * For Bootstrap CICI
 * 
 * Author @semarketir
 * 
 * CICI is simple tool for generate Controller, Model, and View in CodeIgniter ( or CI ) Framework
 * CICI can used to setup configurations in CI ( Database, Autoload, Config ) <- Next Version
 * CICI just can to used for default directory of CodeIgniter ( or CI ) Framework
 * 
 * CICI v.0.0.4 Beta
 * 
 * Support with CI 2.X ( or heigh )
 * 
 * License Under GPL
 * 
 * 
 * Salam, 
 * semar ketir
 * 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

namespace cici
{
    class Checking
    {
        private string[] args;

        private string message;

        private Boolean status = true;

        public Checking()
        {
            this.args = null;
        }
        public Checking(string[] args)
        {
            this.setArgs(args);
        }

        public void execute()
        {

            string string_role = "^("+
						"(create "+
							"("+
                                "(controller ([.a-zA-Z0-9_]+;+)+)" +
                                "||(action ([.a-zA-Z0-9_]+=+[a-zA-Z0-9_]+;+)+)" +
                                "||(model ([.a-zA-Z0-9_]+;+)+)" +
                                "||(modelcrud ([.a-zA-Z0-9_]+;+)+)" +
                                "||(mvc ([.a-zA-Z0-9_]+;+)+)" +
								"||(view ([.a-zA-Z0-9_]+;+)+)"+
							")"+
						")" +
						"||(configure "+
                            "(" +
								"(database ([a-zA-Z0-9_]+[=]+[\"/\\.\\!#\\-\\:_?@&%~ a-zA-Z0-9_ ]*;+)+)" +
								"||(autoload ([a-zA-Z0-9_]+[=]+[\" /.a-zA-Z0-9_]+;+)+)" +
                                "||(routes ([a-zA-Z0-9_]+[=]+[/.a-zA-Z0-9_]*;+)+)" +
                                "||(config ([a-zA-Z0-9_]+[=]+[\"/\\.\\!#\\-\\:_?@&%~ a-zA-Z0-9_ ]*;+)+)" +
							")"+
						")"+
					")$";

            string temp_string=string.Join(" ",this.getArgs());
            try
            {
                Match match = Regex.Match(temp_string, string_role);
                if (match.Success)
                {
                    try
                    {
                        string tempClassName = this.getArgs()[1].Substring(0, 1);
                        string className = tempClassName.ToUpper() + this.getArgs()[1].Remove(0, 1);

                        Type ciciObject = Type.GetType("cici." + className);
                        object ciciInstance = Activator.CreateInstance(ciciObject);

                        object[] ciciParams = new object[] { this.getArgs()[2] };

                        Boolean statusx = (Boolean)ciciObject.InvokeMember("create" + className,
                            BindingFlags.InvokeMethod, null, ciciInstance, ciciParams);

                        if (statusx)
                        {

                            this.setMessage("Success Created");
                        }
                        else
                        {
                            this.setMessage("Failed Created");
                        }

                    }
                    catch (Exception ex)
                    {
                        ex.Message.ToString();

                        this.setStatus(false);
                        this.setMessage("'" + temp_string + "' " + "command not found or command not complete");
                    }
                }
                else
                {
                    this.setStatus(false);
                    this.setMessage("'" + temp_string + "' " + "command not found or command not complete");
                }
            }catch(Exception ex)
            {
                ex.Message.ToString();

                this.setStatus(false);
                this.setMessage("'" + temp_string + "' " + "command not found or command not complete");
            }

        }

        public void setArgs(string[] args)
        {
            this.args = args;
        }

        private string[] getArgs()
        {
            return this.args;
        }

        public Boolean getStatus()
        {
            return this.status;
        }

        private void setStatus(Boolean status)
        {
            this.status = status;
        }

        public string getMessage()
        {
            return this.message;
        }

        public void setMessage(string message)
        {
            this.message = message;
        }
    }
}
